import type { SystemStore } from './index';

// 系统相关选择器
export const systemSelectors = {
  theme: (state: SystemStore) => state.theme,
  language: (state: SystemStore) => state.language,
  isLoading: (state: SystemStore) => state.isLoading,
  error: (state: SystemStore) => state.error,
  hasError: (state: SystemStore) => state.error !== null,
  sidebarCollapsed: (state: SystemStore) => state.sidebarCollapsed,
  mobileMenuOpen: (state: SystemStore) => state.mobileMenuOpen,
  activeTab: (state: SystemStore) => state.activeTab,
  
  isDarkMode: (state: SystemStore) => {
    if (state.theme === 'dark') return true;
    if (state.theme === 'light') return false;
    return window.matchMedia('(prefers-color-scheme: dark)').matches;
  },
  
  activeTheme: (state: SystemStore) => {
    if (state.theme === 'auto') {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    }
    return state.theme;
  },
};

// 用户相关选择器
export const userSelectors = {
  userInfo: (state: SystemStore) => state.userInfo,
  isAuthenticated: (state: SystemStore) => state.isAuthenticated,
  isLoggingIn: (state: SystemStore) => state.isLoggingIn,
  userId: (state: SystemStore) => state.userInfo?.id,
  username: (state: SystemStore) => state.userInfo?.username,
  email: (state: SystemStore) => state.userInfo?.email,
  avatar: (state: SystemStore) => state.userInfo?.avatar,
  userRole: (state: SystemStore) => state.userInfo?.role,
  userStatus: (state: SystemStore) => state.userInfo?.status,
  preferences: (state: SystemStore) => state.preferences,
  permissions: (state: SystemStore) => state.permissions,
  
  isAdmin: (state: SystemStore) => state.userInfo?.role === 'admin',
  isUser: (state: SystemStore) => state.userInfo?.role === 'user',
  isGuest: (state: SystemStore) => state.userInfo?.role === 'guest',
  isActive: (state: SystemStore) => state.userInfo?.status === 'active',
  
  displayName: (state: SystemStore) => {
    return state.userInfo?.username || state.userInfo?.email || '未知用户';
  },
};

// 设置相关选择器
export const settingsSelectors = {
  settings: (state: SystemStore) => state.settings,
  editorSettings: (state: SystemStore) => state.editorSettings,
  displaySettings: (state: SystemStore) => state.displaySettings,
  isSettingsLoaded: (state: SystemStore) => state.isSettingsLoaded,
  settingsVersion: (state: SystemStore) => state.settingsVersion,
  
  layout: (state: SystemStore) => state.settings.layout,
  pageSize: (state: SystemStore) => state.settings.pageSize,
  enableAutoSave: (state: SystemStore) => state.settings.enableAutoSave,
  
  fontSize: (state: SystemStore) => state.editorSettings.fontSize,
  fontFamily: (state: SystemStore) => state.editorSettings.fontFamily,
  tabSize: (state: SystemStore) => state.editorSettings.tabSize,
  
  density: (state: SystemStore) => state.displaySettings.density,
  gridLines: (state: SystemStore) => state.displaySettings.gridLines,
  timestamps: (state: SystemStore) => state.displaySettings.timestamps,
};

// 组合选择器
export const combinedSelectors = {
  appInitialized: (state: SystemStore) => {
    return state.isSettingsLoaded && !state.isLoggingIn;
  },
  
  appConfig: (state: SystemStore) => ({
    theme: systemSelectors.activeTheme(state),
    language: state.language,
    layout: state.settings.layout,
    user: userSelectors.displayName(state),
    isAuthenticated: state.isAuthenticated,
  }),
  
  uiState: (state: SystemStore) => ({
    sidebarCollapsed: state.sidebarCollapsed,
    mobileMenuOpen: state.mobileMenuOpen,
    isLoading: state.isLoading,
    hasError: systemSelectors.hasError(state),
    isDarkMode: systemSelectors.isDarkMode(state),
  }),
};

// 导出所有选择器
export const selectors = {
  ...systemSelectors,
  ...userSelectors,
  ...settingsSelectors,
  ...combinedSelectors,
}; 